package com.zoe.wan.android.example.common.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.RecyclerView
import com.zoe.wan.android.example.R
import com.zoe.wan.android.example.databinding.ItemKnowledgeListBinding
import com.zoe.wan.android.example.databinding.ItemKnowledgeListDetialBinding
import com.zoe.wan.android.example.repositry.data.knowledge.KnowledgeDetialData
import com.zoe.wan.android.example.repositry.data.knowledge.KnowledgeDetialDataData
import com.zoe.wan.android.example.repositry.data.knowledge.KnowledgeListDataItem

class KnowledgeListDetailAdapter :
    RecyclerView.Adapter<KnowledgeListDetailAdapter.KnowledgeListDetailViewHolder>() {


    private var list: List<KnowledgeDetialDataData?>? = null

    private var itemListener: KnowledgeListDetailListener? = null

    interface KnowledgeListDetailListener {
        fun itemClick(item:  KnowledgeDetialDataData)

    }

    fun registerListener(listener: KnowledgeListDetailAdapter.KnowledgeListDetailListener?) {
        this.itemListener = listener
    }


    fun setDataList(lists: List<KnowledgeDetialDataData?>?) {
        lists?.let {
            list = it
            notifyDataSetChanged()
        }
    }


    class KnowledgeListDetailViewHolder(val binding: ItemKnowledgeListDetialBinding) :
        RecyclerView.ViewHolder(binding.root) {


    }

    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): KnowledgeListDetailViewHolder {
        return KnowledgeListDetailViewHolder(
            DataBindingUtil.inflate(
                LayoutInflater.from(parent.context),
                R.layout.item_knowledge_list_detial, parent, false
            )
        )
    }

    override fun getItemCount(): Int {
        return list?.size ?: 0
    }

    override fun onBindViewHolder(holder: KnowledgeListDetailViewHolder, position: Int) {
        list?.let {

            val item = it.get(position)
            holder.binding.item = item

            var desc = ""

            holder.binding.tvSubtitle.text = item?.title


            holder.binding.root.setOnClickListener {
                item?.let {
                    itemListener?.itemClick(item)
                }

            }
        }
    }
}